var xmlHttp
var url = "../project/playSong.php";
var url_cp = "../project/changePublic.php";
var url_index = "../project/index.php";
var url_delete_song = "../project/deleteSong.php";
var url_delete_playlist = "../project/deletePlaylist.php";
var url_add_playlist = "../project/addToPlaylist.php";
var url_remove_song_from_playlist = "../project/removeFromPlaylist.php";

function playsong(param) {
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.onreadystatechange = playsongStateChanged;
    xmlHttp.open("POST", url, true);
    var title = "title=" + document.getElementById("pltable").rows[param].cells[2].innerHTML;
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(title);
}

function playsongsp(param) {
    alert(param);
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.onreadystatechange = playsongStateChanged;
    xmlHttp.open("POST", url, true);
    var title = "title=" + param;
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(title);
}

function playsongStateChanged() {
    if (xmlHttp.readyState == 4){
        var song = JSON.parse(xmlHttp.responseText);
        document.getElementById("playsong").setAttribute("src", "../project/Musica/"+song);
        document.getElementById("playsong").play();
    }
}

function show_create_playlist() {
    location.replace(url_index+"?create_new_playlist=true");
}

function show_configure_playlist() {
    location.replace(url_index+"?configure_playlists=true");
}

function show_playlist(namelist) {
    location.replace(url_index+"?name_playlist="+namelist);
}

function delete_song(id_song) {
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.onreadystatechange = deleteStateChanged;
    xmlHttp.open("POST", url_delete_song, true);
    var song = "delete_song=" + id_song;
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(song);
}

function delete_playlist(playlist_name) {
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.onreadystatechange = deleteStateChanged;
    xmlHttp.open("POST", url_delete_playlist, true);
    var song = "delete_playlist=" + playlist_name;
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(song);
}

function deleteStateChanged() {
    if (xmlHttp.readyState == 4){
	location.replace(location);
    }
}

function remove_from_playlist(id_song) {
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.onreadystatechange = deleteStateChanged;
    var song = url_remove_song_from_playlist + "?name_playlist=" + location.toString().substr(location.toString().search('=')+1) + "&remove_song=" + id_song;
    xmlHttp.open("GET", song, true);
    xmlHttp.send(null);
}

function move_to_playlist(id_song) {
    var selected_playlist = document.getElementById(id_song+"selected_option").options[document.getElementById(id_song+"selected_option").selectedIndex].value;
    if (selected_playlist != "Select an option...") {
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null){
	    alert("Your browser does not support AJAX!");
	    return;
	}
	xmlHttp.onreadystatechange = deleteStateChanged;
	xmlHttp.open("POST", url_add_playlist, true);
	var song = "moved_song=" + id_song +"&playlist_name=" + selected_playlist;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(song);
    }
}

function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        xmlHttp = new XMLHttpRequest();
    }catch(e){
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e){
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function changePublic(name, checked) {
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.open("POST", url_cp, true);
    var value = "id_song="+name+"&public="+checked;
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(value);
}

function printSuggestions(string){
    $("#searchResults").load("../project/search.php", {SearchInput: string});
    $("#searchResults").slideDown();
}